<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Resultado Pesquisa</title>
	</head>
	<body>
		<h1>Resultado pesquisa</h1>
		<?php
			include 'parametros_conexao.php';

			$nomeOrgao	= $_REQUEST[ 'nomeOrgao' ];
			$conexao	= pg_connect( "host=$host port=$porta dbname=$base user=$usuario password=$senha" );
			$orgaos		= pg_query( $conexao, "SELECT * FROM " . $esquema . ".tb_org_orgao orgao WHERE orgao.str_nome_orgao LIKE '%$nomeOrgao%'" );

			if( pg_num_rows( $orgaos ) == 0 ){
				echo "&Oacute;rg&atilde;o n&atilde;o encontrado";

			} else {
				echo	"<form action='orgao_controller.php' methor='post'>" .
							"<table cellspacing='10'>" .
								"<thead>" .
									"<tr>" .
										"<th>Nome</th>" .
										"<th>Sigla</th>" .
										"<th>Tipo</th>" .
										"<th>A&ccedil;&atilde;o</th>" .
									"</tr>" .
								"</thead>" .
								"<tbody>";
				while( $orgaoAtual = pg_fetch_assoc( $orgaos ) ){
					$codigoTipoOrgao	= $orgaoAtual[ 'num_codigo_tipo_orgao_fk' ];
					$tipoOrgao			= pg_query( $conexao,"SELECT tipoOrgao.str_tipo_orgao FROM $esquema.tb_tor_tipo_orgao tipoOrgao WHERE tipoOrgao.num_codigo_pk = $codigoTipoOrgao" );
					$linhaTipoOrgao		= pg_fetch_assoc( $tipoOrgao );

					echo			"<tr>" .
										"<td>" . $orgaoAtual[ 'str_nome_orgao' ] . "</td>" .
										"<td>" . $orgaoAtual[ 'str_sigla_orgao' ] . "</td>" .
										"<td>" . $linhaTipoOrgao[ 'str_tipo_orgao' ] . "</td>" .
										"<td>" .
											"<a href='novo_orgao.php?codigo_orgao=" . $orgaoAtual[ 'num_codigo_pk' ] . "'>Editar</a> " .
											"<a href='excluir_orgao.php?codigo_orgao=" . $orgaoAtual[ 'num_codigo_pk' ] . "'>Excluir</a> " .
											"<a href='novo_orgao.php?codigo_orgao=" . $orgaoAtual[ 'num_codigo_pk' ] . "&somente_leitura=true'>Consultar</a>" .
										"</td>" .
									"</tr>";
				}
				echo			"</tbody>" .
							"</table>" .
						"</form>";
			}
			echo "<br /><a href='index.php'>Voltar</a>";
		?>
	</body>
</html>